﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Trade.Controls;
using Trade.DAL;
using Trade.Model;

namespace Trade
{
    public class Common
    {
        //设置操作按钮的Enabled
        public void SetButtonOperatorGroup(Utility.CRUD dataState, RayButtonGroup btnOperatorGroup)
        {
            if (dataState == Utility.CRUD.None)
            {
                btnOperatorGroup.btnNew.Enabled = true;
                btnOperatorGroup.btnModify.Enabled = true;
                btnOperatorGroup.btnDelete.Enabled = true;
                btnOperatorGroup.btnSave.Enabled = false;
                btnOperatorGroup.btnCancel.Enabled = false;
            }
            else
            {
                btnOperatorGroup.btnNew.Enabled = false;
                btnOperatorGroup.btnModify.Enabled = false;
                btnOperatorGroup.btnDelete.Enabled = false;
                btnOperatorGroup.btnSave.Enabled = true;
                btnOperatorGroup.btnCancel.Enabled = true;
            }
        }

        /// <summary>
        /// 设置窗体中所有控件的启用状态或编辑状态
        /// </summary>
        /// <param name="control">窗体实例的引用</param>
        /// <param name="boolValue">表是状态的bool值</param>
        public void SetControlsState(Control control, bool state)
        {
            foreach (Control con in control.Controls)
            {
                if (con.GetType() == typeof(RayTextBox))  //若是TextBox
                {
                    //((TextBox)con).ReadOnly = !state;  //设置ReadOnly属性
                    ((TextBox)con).Enabled = state;
                }
                if (con.GetType() == typeof(RayComboBox))  //若是ComboBox
                {
                    ((ComboBox)con).Enabled = state;  //设置Enabled属性
                }
                if (con.GetType() == typeof(DateTimePicker))  //若是DateTimePicker
                {
                    ((DateTimePicker)con).Enabled = state; //设置Enabled属性
                }
                if (con.GetType() == typeof(CheckBox))  //若是CheckBox
                {
                    ((CheckBox)con).Enabled = state; //设置Enabled属性
                }
                if (con.GetType() == typeof(RadioButton))  //若是RadioButton
                {
                    ((RadioButton)con).Enabled = state; //设置Enabled属性
                }
                if (con.GetType() == typeof(GroupBox))  //若是GroupBox
                {
                    SetControlsState(con, state);  //递归执行程序
                }
            }
        }

        //将指定科目的子项加载到ComboBox中
        public void ComboBoxBindingDataSource(RayComboBox cmb, string displayColumn, string valueColumn)
        {
            string sqlGetSubjectList = "Select * from SubjectMessage where ParentId = (select SubjectId from SubjectMessage where SubjectCode = @SubjectCode) order by SubjectCode";
            SqlParameter parm = new SqlParameter("@SubjectCode", SqlDbType.VarChar, 30);
            parm.Value = cmb.Tag.ToString();

            DataTable dtSubject = SQLHelper.ExecuteDataTable(SQLHelper.connectionString, CommandType.Text, sqlGetSubjectList, parm);
            BindingSource bs = new BindingSource(dtSubject, null);
            cmb.BeginUpdate();
            cmb.DataSource = bs;
            cmb.DisplayMember = displayColumn;
            cmb.ValueMember = valueColumn;
            cmb.EndUpdate();
        }

        //将Employee表的人员资料加载到相应的ComboBox
        public void EmployeeComboBoxDataBinding(RayComboBox cmb, string displayColumn, string valueColumn)
        {
            string sqlGetEmployeeNameList = "Select * from Employee";

            DataTable dtEmployee = SQLHelper.ExecuteDataTable(SQLHelper.connectionString, CommandType.Text, sqlGetEmployeeNameList, null);
            BindingSource bs = new BindingSource(dtEmployee, null);
            cmb.BeginUpdate();
            cmb.DataSource = bs;
            cmb.DisplayMember = displayColumn;
            cmb.ValueMember = valueColumn;
            cmb.EndUpdate();
        }

        //将Vendor表中的客户资料加载到相应的ComboBox
        public void VendorComboBoxDataBinding(RayComboBox cmb, string displayColumn, string valueColumn)
        {
            string sqlGetVendorNameList = "Select * from Vendor";

            DataTable dtVendor = SQLHelper.ExecuteDataTable(SQLHelper.connectionString, CommandType.Text, sqlGetVendorNameList, null);
            BindingSource bs = new BindingSource(dtVendor, null);
            cmb.BeginUpdate();
            cmb.DataSource = bs;
            cmb.DisplayMember = displayColumn;
            cmb.ValueMember = valueColumn;
            cmb.EndUpdate();
        }

        /// <summary>
        /// 控制可编辑控件的键盘输入，该方法限定控件只可以接收表示非负十进制数的字符
        /// </summary>
        /// <param name="e">为 KeyPress 事件提供数据</param>
        /// <param name="txt">可编辑文本控件</param>
        public void InputNumeric(KeyPressEventArgs e, TextBox txt)
        {
            //在可编辑控件的Text属性为空的情况下，不允许输入".字符"
            if (String.IsNullOrEmpty(txt.Text) && e.KeyChar.ToString() == ".")
            {
                //把Handled设为true，取消KeyPress事件，防止控件处理按键
                e.Handled = true;
            }

            //可编辑控件不允许输入多个"."字符
            if (txt.Text.Contains(".") && e.KeyChar.ToString() == ".")
            {
                e.Handled = true;
            }

            //在可编辑控件中，只可以输入“数字字符”、".字符" 、"字符"(删除键对应的字符)
            if (!Char.IsDigit(e.KeyChar) && e.KeyChar.ToString() != "." && e.KeyChar.ToString() != "")
            {
                e.Handled = true;
            }

            //con.Text = e.KeyChar.ToString();
        }

        /// <summary>
        /// 控制可编辑控件的键盘输入，该方法限定控件只可以接收表示非负整数的字符
        /// </summary>
        /// <param name="e">为 KeyPress 事件提供数据</param>
        public void InputInteger(KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && e.KeyChar.ToString() != "")
            {
                //把Handled设为true，取消KeyPress事件，防止控件处理按键
                e.Handled = true;
            }
        }
    }
}
